package com.ly.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.utils.erp.CommonUtil;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.heart.domain.*;
import com.ly.heart.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Wuzh
 * @Description: 客户转公海，每天凌晨扫描一次：
 * 1）正常的客户，超过系统设置的天数未回访的转为公海客户
 * 2）正常的客户，没有回访记录的看客户的创建时间
 * 2）转成后站内信通知系统设置里的操作员
 * @Date Created in 21:01 2024-09-10
 * @return
 */
@Component
@Slf4j
public class CustomerTask {

    @Resource
    private ICustomerService iCustomerService;
    @Resource
    private ICustomerVisitRecordService iCustomerVisitRecordService;
    @Resource
    private ISysSetupService iSysSetupService;

    /**
     * 扫描转账单
     */
    public void scanCustomer () {
        log.info ( "启动定时任务，扫码客户转为公海客户" );
        //查询有效客户
        CustomerVisitRecordEntity recordEntity = new CustomerVisitRecordEntity ();
        List<CustomerVisitRecordEntity> list = iCustomerVisitRecordService.queryList ( recordEntity );
        log.info ( "有效的常规客户数-》{}", list.size () );
        if ( list == null || list.size () <= 0 ) {
            return;
        }
        String days = "0";
        //统计超过系统设置的天数未回访的
        List<SysSetupEntity> sysSetupEntities = iSysSetupService.list ( new LambdaQueryWrapper<SysSetupEntity> ().eq ( SysSetupEntity :: getBizType, 1 ) );
        if ( sysSetupEntities != null && sysSetupEntities.size () > 0 ) {
            SysSetupEntity sysSetupEntity = sysSetupEntities.stream ().filter ( set -> set.getDicKey ().equals ( "DAYS" ) ).findFirst ().get ();
            if ( sysSetupEntity != null ) {
                days = sysSetupEntity.getDicValue (); //超过该天数未回访的，转为公海客户
            }
        }
        if ( ! days.equals ( "0" ) ) {
            convert ( list, days );
        }
    }

    /**
     * 将正常客户转为公海客户，修改客户类型即可
     *
     * @param customerEntities：正常客户集合
     * @param days：超过的天数
     */
    private void convert ( List<CustomerVisitRecordEntity> customerEntities, String days ) {
        customerEntities.forEach ( customerEntity -> {
            //检查每位是否有回放
            Date date = customerEntity.getVisitTime () == null ? customerEntity.getCreateCustomerTime () : customerEntity.getVisitTime ();
            int day = CommonUtil.timeDifferenceDay ( date, new Date () );
            if ( day >= Integer.parseInt ( days ) ) {
                // 超过系统设置的天数未回访，转为公海客户
                boolean update = iCustomerService.update ( new LambdaUpdateWrapper<CustomerEntity> ().
                        set ( CustomerEntity :: getType, ConstUtil.CUSTOMER_SEA ).
                        eq ( CustomerEntity :: getId, customerEntity.getId () ) );
                if ( ! update ) {
                    log.error ( "id为【{}】的客户【{}】，转换公海客户失败！", customerEntity.getId (), customerEntity.getCustomerName () );
                }
                log.info ( "id为【{}】的客户【{}】，转换公海客户成功", customerEntity.getId (), customerEntity.getCustomerName () );
            }
        } );
    }

    private void notice () {

    }

}
